package bavaria.hightec.time;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import bavaria.hightec.banking.BankImpl;

public class TimeEmitter {
	
	private static TimeEmitter theInstance;
	static Calendar c ;
	public static final int TIMEFAKTOR=604800; //durchschnittlicher Monat im Jahr in sec bei 365 Tagen
	
	private TimeEmitter(){
		c = Calendar.getInstance();
		startTiming();
	}
	
	public static TimeEmitter getInstance(){
		if(theInstance==null)
			theInstance=new TimeEmitter();
		return theInstance;
	}
	
	private void modifyCalendar(){
		c.add(Calendar.SECOND, TIMEFAKTOR);
	}
	
	public static Calendar getCalendar(){
		Calendar c1 = (Calendar)c.clone();
		return c1;
	}
	
	private void startTiming() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(
            new TimerTask()  {
                public void run()  {
                   TimeEmitter.this.modifyCalendar();
                }
            },
             0,
            1000);
        
        Clock c = new Clock(timer, this.c);
    }
	
	
	
	public static double getPeriod(){
		double period = ((double)BankImpl.INTERESTTIMEFAKTOR/(double)TIMEFAKTOR)*(1000);
		return  period;
	}
}
